<?php

namespace app\admin\model;
use think\Model;


class Article extends Model
{

    

    

    // 表名
    protected $name = 'article';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'type_text',
        'status_text'
    ];
    

    protected static function init()
    {
        self::afterInsert(function ($row) {
            if (!$row['weigh']) {
                $pk = $row->getPk();
                $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
            }
        });
    }

    
    public function getTypeList()
    {
        return ['UserInfo' => __('UserInfo'), 'INTRODUCTION' => __('INTRODUCTION')];
    }

    public function getStatusList()
    {
        return ['1' => __('Status 1'), '0' => __('Status 0')];
    }


    public function getTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['type']) ? $data['type'] : '');
        $list = $this->getTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    /**
     * 获取某一类型的文章
     * @param $type
     * @param $zone_id
     * @param $client
     * @return ArticleModel|array|bool|\PDOStatement|string|Model
     */
    public static function getOneByType($type, $zone_id = 0, $client = 'CUSTOMER')
    {
        $item = self::where('type', $type)->order('id', 'desc')->find();
        if ($item) {
            return $item;
        }
        return self::create([
            'client'  => $client,
            'type'    => $type,
            'zone_id' => $zone_id,
            'name'    => '',
            'content' => '',
            'status'  => 1,
            'weigh'   => 0,
        ]);
    }


}
